﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mars_server.csharp.model;
using MySql.Data.MySqlClient;
using mars_server.csharp.DB;
using mars_server.csharp.Util;

namespace mars_server.csharp.dao.impl
{
    public class GroupManagerImpl : GroupManager
    {

        private GroupManagerImpl() { }
        private static readonly GroupManagerImpl instance = new GroupManagerImpl();
        public static GroupManagerImpl Instance
        {
            get
            {
                return instance;
            }
        }

        ConnectionPool pool = ConnectionPool.getInstance();//mysql连接池，单例模式

        string tableName = "mars_group";
        public GroupDO selectByPrimaryKey(long id)
        {
            MySqlConnection connection = pool.getConnection();
            string query = SqlUtil.selectByPrimaryKey(tableName, id);
            try
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    GroupDO groupDO = new GroupDO();
                    this.dataReaderToGroupDO(groupDO, dataReader);
                    pool.releaseConnection(connection);
                    dataReader.Close();
                    return groupDO;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());

            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return null;
        }

        public bool insertGroupDO(GroupDO groupDO)
        {
            if(groupDO.groupIntro == "")
            {
                groupDO.groupIntro = "暂无数据";
            }

            string query = "insert into mars_group values (" + "NULL" + ",'" + groupDO.groupName + "','" + groupDO.groupIntro +"','"+DateTime.Now.ToString()+"',"+"NULL)";
            Console.WriteLine(query);

            MySqlConnection connection = pool.getConnection();
            try
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                pool.releaseConnection(connection);
                return false;
            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return true;
        }

        public List<GroupDO> selectGroupDOsByGroupIds(List<long> groupIds)
        {
            List<GroupDO> groupDOs = new List<GroupDO>();
            string query = SqlUtil.selectByIds(tableName, groupIds);
            MySqlConnection connection = pool.getConnection();

            try
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    GroupDO groupDO = new GroupDO();
                    dataReaderToGroupDO(groupDO, dataReader);
                    groupDOs.Add(groupDO);
                }
                dataReader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return groupDOs;
        }
        public List<GroupDO> selectGroupDOByQuery(Dictionary<string, string> sqlMap)
        {
            MySqlConnection connection = pool.getConnection();
            List<GroupDO> groupDOs = new List<GroupDO>();
            string filed = "", value = "";
            foreach (var map in sqlMap)
            {
                filed = map.Key;
                value = map.Value;
                break;
            }
            string query = SqlUtil.selectByStringQueryRegexp(tableName, filed, value);
            try
            {
               
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    GroupDO groupDO = new GroupDO();
                    this.dataReaderToGroupDO(groupDO, dataReader);
                    groupDOs.Add(groupDO);
                }
                pool.releaseConnection(connection);
                dataReader.Close();
                return groupDOs;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return groupDOs;
        }

        public void dataReaderToGroupDO(GroupDO groupDO, MySqlDataReader dataReader)
        {
            groupDO.id = long.Parse(dataReader[0].ToString());
            groupDO.groupName = dataReader[1].ToString();
            groupDO.groupIntro = dataReader[2].ToString();
            groupDO.gmtGreate = dataReader[3].ToString();
            groupDO.gmtModified = dataReader[4].ToString();
        }
    }
}
